package user

import (
	"context"
	"errors"
	"go-zero-all-demos/apps/user/rpc/userservice"

	"go-zero-all-demos/apps/api/internal/svc"
	"go-zero-all-demos/apps/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeleteUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteUserLogic {
	return &DeleteUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeleteUserLogic) DeleteUser(req *types.UserID) error {
	user, err := l.svcCtx.UserRPC.GetUserByID(l.ctx, &userservice.UserID{Id: req.Id})
	if err != nil {
		return err
	}
	if user == nil || user.Id != req.Id {
		return errors.New("invalid user")
	}
	_, err = l.svcCtx.UserRPC.DeleteUser(l.ctx, &userservice.UserID{Id: req.Id})
	return err
}
